//
//  PlaceOrderViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/24.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "PlaceOrderViewController.h"
#import <MBProgressHUD.h>
#import "Partner.h"
#import "BLHeader.h"
#import "VervifyPhone.h"
#import "OrderNet.h"
#import "CustomerOrderDetailViewController.h"
#import "ChoiceStoreViewController.h"
#import "ScanCustomerInfoViewController.h"
#import "QHKeyboard.h"

@interface PlaceOrderViewController ()

@property (weak, nonatomic) IBOutlet UIButton *customerNameLabel;

@property (weak, nonatomic) IBOutlet UITextField *customerPhoneLable;
@property (weak, nonatomic) IBOutlet UITextField *addressLabel;

@property (weak, nonatomic) IBOutlet UITextField *saleNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *salePhoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *storeNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *storePhoneLabel;
@property (weak, nonatomic) IBOutlet UITextView *remarksTextView;
@property (weak, nonatomic) IBOutlet UIButton *orederBtn;
@property (weak, nonatomic) IBOutlet UITextField *orderTheme;
@property (weak, nonatomic) IBOutlet UITextField *orderTotalPrice;
@property (weak, nonatomic) IBOutlet UIButton *pay1;
@property (weak, nonatomic) IBOutlet UIButton *pay2;
@property (weak, nonatomic) IBOutlet UIButton *pay3;
@property (nonatomic, assign) NSInteger payWay;
@property (weak, nonatomic) IBOutlet UITextField *storeAddressText;




@end

@implementation PlaceOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.orederBtn.layer.cornerRadius = 5;
    self.title = @"创建订单";
    NSString *saleName = [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME];
    NSString *saleTel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
    self.saleNameLabel.text = saleName;
    self.salePhoneLabel.text = saleTel;
    
    self.orderTotalPrice.inputView = [[QHKeyboard alloc] init];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(createOrder)];
    [right setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:kAppThemeColor ,NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = right;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:tap];
    _payWay = 1;
    self.pay1.selected = YES;
    UITapGestureRecognizer *storeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toChoiceStore)];
    self.storeNameLabel.userInteractionEnabled = YES;
    [self.storeNameLabel addGestureRecognizer:storeTap];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getStore:) name:@"choiceStore" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCustomer:) name:@"scanSuccess" object:nil];
}

- (void)getCustomer:(NSNotification *)notification {
    NSDictionary *dic = notification.object;
    [self.customerNameLabel setTitle:[dic objectForKey:@"userName"] forState:UIControlStateNormal];
    self.customerPhoneLable.text = [dic objectForKey:@"customTel"];
    NSString *address = [dic objectForKey:@"userAddr"];
    if (address && [address length] > 0) {
        self.addressLabel.text = address;
    }
}

- (void)getStore:(NSNotification *)notification {
    Partner *part = [notification object];
    [self.storeNameLabel setTitle:part.name forState:UIControlStateNormal];
    self.storePhoneLabel.text = part.tel;
    if (part.userAddr) {
        self.storeAddressText.text = part.userAddr;
    }
}

// 选择店主界面
- (void)toChoiceStore {
    ChoiceStoreViewController *sell = [[ChoiceStoreViewController alloc] init];
    [self.navigationController pushViewController:sell animated:YES];
}


- (void)hideKeyBoard {
    [self.view endEditing:YES];
}

- (IBAction)commitOrder:(id)sender {
    [self createOrder];
}

- (void)createOrder {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *customerName = self.customerNameLabel.titleLabel.text;
    hud.mode = MBProgressHUDModeText;
    if(!customerName || [customerName length] == 0) {
        hud.label.text = @"请输入客户名！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    // 考虑到又座机的
    NSString *customerTel = self.customerPhoneLable.text;
    if(!customerTel || [customerName length] == 0) {
        hud.label.text = @"请输入客户电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    if([customerTel length] != 7 && [customerTel length] != 11) {
        hud.label.text = @"请输入规范的客户电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *address = self.addressLabel.text;
    if (!address || [address length] == 0) {
        hud.label.text = @"请客户输入收货地址！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *theme = self.orderTheme.text;
    if (!theme || [theme length] == 0) {
        hud.label.text = @"请输入订单主题";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *price = self.orderTotalPrice.text;
    if (!price || [price length] == 0 ) {
        hud.label.text = @"请输入工程总价！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *desc = self.remarksTextView.text;
    if (!desc || [desc length] == 0 ) {
        hud.label.text = @"请输入订单描述！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    // 销售信息
    NSString * saleName = self.saleNameLabel.text;
    if(!saleName || [saleName length] == 0) {
        hud.label.text = @"请销售客户名！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    // 考虑到又座机的
    NSString *saleTel = self.salePhoneLabel.text;
    if(!saleTel || [saleTel length] == 0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入销售电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    if([saleTel length] != 7 && [saleTel length] != 11) {
        hud.label.text = @"请输入规范的销售电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    // 店主信息
    NSString * storeName = [[self.storeNameLabel titleLabel] text];
    if(!storeName || [storeName length] == 0) {
        hud.label.text = @"请店主客户名！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    // 考虑到又座机的
    NSString *storeTel = self.storePhoneLabel.text;
    if(!storeTel || [storeTel length] == 0) {
        hud.label.text = @"请输入店主电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    if([storeTel length] != 7 && [storeTel length] != 11) {
        hud.label.text = @"请输入规范的店主电话！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    NSString *storeAddress = self.storeAddressText.text;
    if (!storeAddress || [storeAddress length] == 0) {
        hud.label.text = @"请输入店主地址！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    hud.mode = MBProgressHUDModeIndeterminate;
    __weak typeof (self) weakSelf = self;
    NSDictionary *dictionary = @{@"customerName" : customerName, @"customerTel" : customerTel,
                                 @"saleName" : saleName, @"saleTel" : saleTel,
                                 @"supplierName" : storeName, @"supplierTel" : storeTel,
                                 @"payWay" : [NSString stringWithFormat:@"%li",_payWay],
                                 @"orderTitle" : theme, @"orderPrice" : price,
                                 @"orderInfo" : desc, @"deliveryAddress" : address, @"supAddress" : storeAddress};
    [OrderNet salePlaceOrder:dictionary completer:^(id result, NSError *error) {
        if(result) {
            [hud hideAnimated:YES];
            CustomerOrderDetailViewController *controller = [[CustomerOrderDetailViewController alloc] init];
            controller.order = result;
            [weakSelf.navigationController pushViewController:controller animated:YES];
            
            NSArray *controllers = self.navigationController.viewControllers;
             NSMutableArray *newControllers = [NSMutableArray arrayWithArray:controllers];
            [newControllers removeObjectAtIndex:controllers.count - 2];
            [self.navigationController setViewControllers:[newControllers copy] animated:YES];
            //CustomerOrderDetailViewController *controller = [[CustomerOrderDetailViewController alloc] init];
            

        }else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }
    }];
}

- (IBAction)toScanCustomInfo:(UIButton *)sender {
    ScanCustomerInfoViewController *vc = [[ScanCustomerInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)payWay1:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    [_pay1 setSelected:YES];
    [_pay2 setSelected:NO];
    [_pay3 setSelected:NO];
    _payWay = 1;
}

- (IBAction)payWay2:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    [_pay1 setSelected:NO];
    [_pay3 setSelected:NO];
    [_pay2 setSelected:YES];
    _payWay = 2;
}

- (IBAction)payWay3:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    [_pay1 setSelected:NO];
    [_pay2 setSelected:NO];
    [_pay3 setSelected:YES];
    _payWay = 3;
}

@end
